<script setup lang="ts">
import { reactive, ref } from 'vue';
import logo from '@/assets/logo.png';
import type { FormInstance, FormRules } from 'element-plus';
import { useUserStore } from '@/store/auth';
import { useRouter } from 'vue-router';
const ruleFormRef = ref<FormInstance>();
const userStore = useUserStore();
const router = useRouter()
console.log("🚀 ~ userStore:", userStore)
interface RuleForm {
  username: string;
  password: string;
}

const ruleForm: RuleForm = reactive({
  username: '',
  password: '',
});

const rules = reactive<FormRules<RuleForm>>({
  username: [
    { required: true, message: '请输入账户名', trigger: 'blur' },
    {
      min: 4,
      max: 8,
      message: '用户名长度为4到8位',
      trigger: 'blur',
    },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    // {
    //     pattern:/^\d{6}$/, message:'密码必须是6位纯数字', trigger: 'blur'
    // }
  ],
});

const handleLogin = async () => {
  ruleFormRef.value?.validate(async(valid: boolean) => {
    console.log('🚀 ~ ruleFormRef.value?.validate ~ valid:', valid);
    if (valid) {
      // 校验通过
     await userStore.login(ruleForm)
      router.push('/')
    }
  });
};
</script>

<template>
  <div class="bg">
    <div class="login">
      <div class="logo">
        <img :src="logo" alt="" width="70px" height="70px" />
        <h1 class="ml">动力港能源管理平台</h1>
      </div>
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules">
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            placeholder="请输入账号"
            prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            type="password"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="handleLogin"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped lang="less">
.bg {
  background-image: url('../assets/bg.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 100vh;
  .login {
    width: 500px;
    height: 300px;
    padding: 50px;
    box-shadow: 0 0 10px 10px #f4f4f4;
    text-align: center;
    position: absolute;
    top: 50%;
    margin-top: -200px;
    left: 10%;

    .logo {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 40px;
      h1 {
        color: rgb(14, 53, 148);
      }
    }
  }
}
</style>
